package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservation;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {

    private final CustomerReservation customerReservation;

    @GetMapping("/cancelled-count")
    @ApiOperation("获取取消预约次数")
    public ResponseResult cancel() {
        Long userId = UserThreadLocal.getUserId();
        Integer cancelCount = customerReservation.getCancelCount(userId);
        return success(cancelCount);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询某个时间段的用户预约人数")
    public ResponseResult<List<TimeCountVo>> countByTime(
            @ApiParam(value = "时间") Long time
    ) {
        List<TimeCountVo> list = customerReservation.countByTime(time);
        return success(list);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult insert(
            @ApiParam(value = "预约信息") @RequestBody ReservationDto reservationDto
    ) {
        customerReservation.insert(reservationDto);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> pageList(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "状态", defaultValue = "0") @RequestParam(defaultValue = "0") Integer status
    ) {
        PageResponse<ReservationVo> pageResponse = customerReservation.pageList(pageNum, pageSize, status);
        return success(pageResponse);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消一个预约")
    public ResponseResult cancel(
            @ApiParam(value = "预约ID") @PathVariable Long id
    ) {
        customerReservation.cancel(id);
        return success();
    }
}